package cn.yunsenlin.formula.cloud.share;

import cn.yunsenlin.formula.cloud.bean.auth.Template;
import cn.yunsenlin.formula.cloud.bean.result.Result;
import cn.yunsenlin.formula.cloud.bean.result.ResultRequest;
import cn.yunsenlin.formula.cloud.bean.template.TemplateTest;
import cn.yunsenlin.formula.cloud.interfaces.ResultService;
import cn.yunsenlin.formula.cloud.interfaces.ShareGroupService;
import cn.yunsenlin.formula.cloud.interfaces.ShareResultService;
import cn.yunsenlin.formula.cloud.interfaces.ShareTemplateService;
import cn.yunsenlin.formula.cloud.mysql.bean.generator.TbResultContent;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShareResultServiceImpl implements ShareResultService{
    private final ResultService resultService;
    private final ShareTemplateService shareTemplateService;
    private final ShareGroupService shareGroupService;

    public ShareResultServiceImpl(ResultService resultService, ShareTemplateService shareTemplateService, ShareGroupService shareGroupService) {
        this.resultService = resultService;
        this.shareTemplateService = shareTemplateService;
        this.shareGroupService = shareGroupService;
    }

    @Override
    public String checkValidWithTemplateUuid(List<TbResultContent> tbResultContentList, String templateUuid, String symbol) {
        Template template = shareTemplateService.selectByUuid(templateUuid);
        if(template!=null){
            return resultService.checkValidWithTemplateId(tbResultContentList,template.getBaseInfo().getId(),symbol);
        }
        return "error";
    }

    @Override
    public Result analyze(List<TbResultContent> tbResultContentList, String templateUuid) {
        Template template = shareTemplateService.selectByUuid(templateUuid);
        if(template!=null){
            if(template.getBaseInfo().getShareStorage()){
                ResultRequest request = new ResultRequest();
                request.setResultContentList(tbResultContentList);
                request.setTemplateId(template.getBaseInfo().getId());
                request.setGroupId(shareGroupService.getGroupByTemplateUuid(templateUuid).getId());
                return resultService.analyze(request);
            }else{
                template.getBaseInfo().setId(null);
                TemplateTest templateTest = new TemplateTest();
                templateTest.setResultContents(tbResultContentList);
                templateTest.setTemplate(template);
                return resultService.templateTest(templateTest);
            }
        }
        return null;
    }
}
